
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
import 'package:dolqun_movies/UI/Pages/Models/MainModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/WechatApiModel.dart';
import 'package:dolqun_movies/UI/Pages/ProfilePage/BuyVipPage.dart';
import 'package:dolqun_movies/UI/Pages/ProfilePage/DownloadPage.dart';
import 'package:dolqun_movies/UI/Widgets/MovieView.dart';
import 'package:dolqun_movies/UI/Widgets/MyAnimatedRoute.dart';
import 'package:dolqun_movies/UI/Widgets/MyEdgeClipper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wechat_kit/wechat_kit.dart' as wechatKit;
import 'Models/AnimationKeysModel.dart';

class ProfilePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> with AutomaticKeepAliveClientMixin<ProfilePage> {
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    var keyModel = ModelContainer.of<AnimationKeysModel>(context).model;

    var keyOfParent = keyModel.settingsIconKeys[0].keyOfParent;
    keyModel.shareIconKeys[0].keyOfParent = keyOfParent;
    keyModel.cartIconKeys[0].keyOfParent = keyOfParent;
    keyModel.favoriteIconKeys[0].keyOfParent = keyOfParent;

    Widget buildingWidget = Scaffold(
      key: keyOfParent,
      backgroundColor: Colors.transparent,
      body: ModelDescendant<WechatApiModel>(
        onChanged: (context, _, wechatApiModel){
          var hasSignIn = wechatApiModel.userInfo != null;

          return CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
                backgroundColor: Colors.transparent,
                expandedHeight: 290.0,
                flexibleSpace: FlexibleSpaceBar(
                  background: ClipPath(
                    clipper: MyEdgeClipper(
                        clipHeight: 25.0
                    ),
                    child: Stack(
                      children: <Widget>[
                        Positioned.fill(
                          child: Image.asset("assets/images/user_top_bg.jpg", fit: BoxFit.cover,)//CachedNetworkImage(imageUrl: "https://cdn.pixabay.com/photo/2016/10/21/14/50/plouzane-1758197_960_720.jpg", fit: BoxFit.cover,),
                        ),
                        Positioned(
                          bottom: 0.0,
                          right: 0.0,
                          left: 0.0,
                          child: SizedBox(
                            height: 80.0,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Container(
                                  decoration: BoxDecoration(
                                      color: Colors.blue,
                                      borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(20.0),
                                        topRight: Radius.circular(20.0),
                                      )
                                  ),
                                  width: 140.0,
                                  height: double.infinity,
                                  alignment: Alignment.topCenter,
                                  child: FlatButton(
                                    textColor: Colors.white,
                                    onPressed: (){
                                      wechatApiModel.wechatApi.shareWebpage(scene: wechatKit.WechatScene.TIMELINE, webpageUrl: "app.dolqun.net");
                                    },
                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: <Widget>[
                                        Icon(Icons.share, color: Colors.white,),
                                        SizedBox(width: 5.0,),
                                        Text("ھەمبەھىرلەڭ")
                                      ],
                                    ),
                                  )
                                ),
                                SizedBox(width: 50.0,),
                                Container(
                                  decoration: BoxDecoration(
                                      color: Colors.orange,
                                      borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(20.0),
                                        topRight: Radius.circular(20.0),
                                      )
                                  ),
                                  width: 140.0,
                                  height: double.infinity,
                                  alignment: Alignment.topCenter,
                                  child: FlatButton(
                                      textColor: Colors.white,
                                      onPressed: (){
                                        Navigator.of(context).push(MyAnimatedRoute(builder: (context) => BuyVipPage()));
                                      },
                                      child: Row(
                                        mainAxisSize: MainAxisSize.min,
                                        children: <Widget>[
                                          Image.asset("assets/images/vip.png", width: 25.0,),
                                          SizedBox(width: 5.0,),
                                          Text("ئالاھىدە ئەزالىق"),
                                        ],
                                      )
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                        Positioned.fill(
                          child: Center(
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                if (!hasSignIn) ClipRRect(
                                  child: BackdropFilter(
                                    filter: ImageFilter.blur(sigmaY: 10.0, sigmaX: 10.0),
                                    child: Container(
                                      width: 130.0,
                                      height: 130.0,
                                      color: Colors.white.withOpacity(0.1),
                                      alignment: Alignment.center,
                                      child: Image.asset("assets/images/Account.png", width: 80.0),
                                    ),
                                  ),
                                  borderRadius: BorderRadius.circular(65.0),
                                ),
                                if (hasSignIn) CircleAvatar(
                                  radius: 60.0,
                                  backgroundImage: NetworkImage(wechatApiModel.userInfo.data.avatarurl),
                                ),
                                if (!hasSignIn) RaisedButton(
                                  child: Text("كىشىڭ"),
                                  onPressed: () async {
                                    wechatApiModel.signInWithWechat();
                                  },
                                ),
                                if (hasSignIn) Text(wechatApiModel.userInfo.data.nickname, style: TextStyle(fontSize: 16.0),),
                                SizedBox(height: 50.0,)
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildListDelegate([
                  ListTile(
                    leading: Icon(Icons.cloud_download, size: 34.0, color: Colors.blue,),
                    title: Text("چۈشۈرۈلمىلەر", style: TextStyle(color: Colors.blue),),
                    subtitle: Text("سىز چۈشۈرگەن چۈشۈرۈلمىلەر، قويۇش قالدۇقلىرى...", style: TextStyle(color: Colors.blue),),
                    onTap: (){
                      Navigator.of(context).push(MyAnimatedRoute(
                          builder: (context) => DownloadPage()
                      ));
                    },
                  ),
                  Row(
                    children: <Widget>[
                      SizedBox(width: 75.0,),
                      Text("كۆرۈش خاتېرىسى", style: TextStyle(color: Colors.blue),),
                    ],
                  ),
                  SizedBox(height: 10.0,),
                  SizedBox(
                    height: 200.0,
                    child: Stack(
                      children: <Widget>[
                        Positioned.fill(
                          child: ModelDescendant<MainModel>(
                            onChanged: (context, _, model) => ListView(
                              scrollDirection: Axis.horizontal,
                              children: [
                                SizedBox(width: 68.0,),
                                ...(model.homeList?.data ?? [])[0].items.map((item) => MovieView(
                                  movie: item,
                                  onTab: (){

                                  },
                                )).toList(),
                                SizedBox(width: 60.0,),
                              ],
                            ),
                          ),
                        ),
                        Positioned(
                          top: 0,
                          bottom: 0,
                          right: 0,
                          child: ClipRRect(
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(25.0),
                              bottomLeft: Radius.circular(25.0),
                            ),
                            child: BackdropFilter(
                              filter: ImageFilter.blur(sigmaY: 10.0, sigmaX: 10.0),
                              child: Container(
                                width: 50.0,
                                height: double.infinity,
                                color: Colors.white.withOpacity(0.04),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                                  children: <Widget>[
                                    Container(width: double.infinity, height: 50.0, color: Colors.transparent, key: keyModel.favoriteIconKeys[0].keyOfWidget,),
                                    Container(width: double.infinity, height: 50.0, color: Colors.transparent, key: keyModel.shareIconKeys[0].keyOfWidget,),
                                    Container(width: double.infinity, height: 50.0, color: Colors.transparent, key: keyModel.cartIconKeys[0].keyOfWidget,),
                                    Container(width: double.infinity, height: 50.0, color: Colors.transparent, key: keyModel.settingsIconKeys[0].keyOfWidget,),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ]),
              )
            ],
          );
        },
      ),
    );
    return buildingWidget;
  }

  @override
  bool get wantKeepAlive => true;
}
